#!/bin/bash
vdbenchPath="/root/vdbench50407"
jenkinsHome="/home/hzz_jenkins"
commonFunc="${jenkinsHome}/common/common_func.sh"
fsFunc="${jenkinsHome}/common/fs_func.sh"
duFunc="${jenkinsHome}/common/du_func.sh"

MountPoint="/mnt/orcafs"
SysUser="quota_user_1"
HzzUser="hzz"
set -e

function test_client()
{
    if mount -t orcafs &> /dev/null  && [ -d ${MountPoint} ]; then
        echo -e "\tOrcafs mount success : ${MountPoint}"
    else
        echo "Orcafs mount failed"
        return 1;
    fi
}

function test_sys_user()
{
    if ! id "${SysUser}" &> /dev/null; then useradd -s /bin/bash -U -m "${SysUser}"; fi
    SysUid=`cat /etc/passwd | grep "${SysUser}" | awk -F: '{print $3}'`
    SysGid=`cat /etc/passwd | grep "${SysUser}" | awk -F: '{print $4}'`

    if [ "${SysUid}"X == ""X ] || [ "${SysGid}"X == ""X ]; then
        echo "Invalid uid=${SysUid}, gid=${SysGid}"; return 1
    fi

    if ! id "${HzzUser}" &> /dev/null; then useradd -s /bin/bash -U -m "${HzzUser}"; fi
    HzzUid=`cat /etc/passwd | grep "${HzzUser}" | awk -F: '{print $3}'`
    HzzGid=`cat /etc/passwd | grep "${HzzUser}" | awk -F: '{print $4}'`

    if [ "${HzzUid}"X == ""X ] || [ "${HzzGid}"X == ""X ]; then
        echo "Invalid uid=${HzzUid}, gid=${HzzGid}"; return 1
    fi
}

function test_common_func()
{
    if [ ! -f "${commonFunc}" ]; then 
        echo "Fail to source_common_func"; return 1; 
    fi
    
    if [ ! -f "${fsFunc}" ]; then 
        echo "Fail to source_fs_func"; return 1; 
    fi

    if [ ! -f "${duFunc}" ]; then 
        echo "Fail to source_fs_func"; return 1; 
    fi

    # shellcheck source=./common_func.sh
    source ${commonFunc}
    # shellcheck source=./fs_func.sh
    source ${fsFunc}
    # shellcheck source=./du_func.sh
    source ${duFunc}
}

function test_storage_pool()
{
    if orcafs-ctl --liststoragepools --id=2 | grep "Targets " &> /dev/null; then
        echo ""
    else
        echo "Error: Storage pool id (2) is not exist"
        return 1;
    fi
}

function check_env()
{
    test_common_func
    test_client
    test_sys_user
    test_storage_pool
}

check_env

